package com.fl.myRule;

/**
 * @Description: TODO
 * @author: fulin
 * @date: 2021年04月25日 16:46
 */
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class FlRandomRule extends AbstractLoadBalancerRule {
    //每个服务访问5次，换下一个服务(2个)

    //total=0, 默认=0,如果=5,我们指向下个服务节点
    //index=0,默认=0，如果index=2，index+1
    private int total = 0;//被调用的次数
    private int currentIndex = 0;//当前提供服务是谁
    @SuppressWarnings({"RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"})
    public Server choose(ILoadBalancer lb, Object key) {
        if (lb == null) {
            return null;
        } else {
            Server server = null;

            while(server == null) {

                if (Thread.interrupted()) {
                    return null;
                }

                //获得还活着的服务
                List<Server> upList = lb.getReachableServers();
                //获得全部的服务
                List<Server> allList = lb.getAllServers();
                int serverCount = allList.size();
                if (serverCount == 0) {
                    return null;
                }
//                int index = this.chooseRandomInt(serverCount);//生成随机数
//                server = (Server)upList.get(index); //从活着的服务中随机获取一个
                //-========================================================
                if (total<5){
                    server = upList.get(currentIndex);
                    total++;
                }else {
                    total = 0;
                    currentIndex++;
                    if (currentIndex>=upList.size()){
                        currentIndex = 0;
                    }
                    server = upList.get(currentIndex);//从活着的服务中，获取指定的服务来进行
                }



                //-========================================================
                if (server == null) {
                    Thread.yield();
                } else {
                    if (server.isAlive()) {
                        return server;
                    }

                    server = null;
                    Thread.yield();
                }
            }

            return server;
        }
    }

    protected int chooseRandomInt(int serverCount) {
        return ThreadLocalRandom.current().nextInt(serverCount);
    }

    public Server choose(Object key) {
        return this.choose(this.getLoadBalancer(), key);
    }

    public void initWithNiwsConfig(IClientConfig clientConfig) {
    }
}

